<?php

namespace app\api\model\order;

use think\Model;


class OrderMaster extends Model
{

    

    

    // 表名
    protected $name = 'order_master';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_type_text',
        'pay_status_text',
        'pay_time_text',
        'status_text',
        'report_status_text',
        'quyangtime_text',
        'songjiantime_text',
        'overtime_text'
    ];
    
    public function getPayTypeList()
    {
        return ['10' => __('Pay_type 10'), '20' => __('Pay_type 20')];
    }

    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '1' => __('Pay_status 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '10' => __('Status 10'), '20' => __('Status 20'), '30' => __('Status 30'), '-10' => __('Status -10'), '-20' => __('Status -20')];
    }

    public function getReportStatusList()
    {
        return ['0' => __('Report_status 0'), '10' => __('Report_status 10'), '20' => __('Report_status 20'), '30' => __('Report_status 30')];
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['pay_type'] ?? '');
        $list = $this->getPayTypeList();
        return $list[$value] ?? '';
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['pay_status'] ?? '');
        $list = $this->getPayStatusList();
        return $list[$value] ?? '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['pay_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        if($value == 30 &&  $data['report_status'] == 10){
            return '待检测';
        }else if($value == 30 &&  $data['report_status'] == 20){
            return '检测中';
        }else if($value == 30 &&  $data['report_status'] == 25){
            return '部分完成';
        }else if($value == 30 &&  $data['report_status'] == 30){
            return '已完成';
        }
        return $list[$value] ?? '';
    }


    public function getReportStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['report_status'] ?? '');
        $list = $this->getReportStatusList();
        return $list[$value] ?? '';
    }


    public function getQuyangtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['quyangtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSongjiantimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['songjiantime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOvertimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['overtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function ordergoodstitle(){
        return $this->hasMany('OrderGoods','order_master_id','id')->field('title,order_master_id');
    }

    public function ordergoods(){
        return $this->hasMany('OrderGoods','order_master_id','id');
    }

    public function hospital()
    {
        return $this->hasOne('\app\api\model\hospital\Hospital', 'id', 'hospital_id')->field('id,title as hospital_name')->bind(['hospital_name']);
    }

    public function hospital2()
    {
        return $this->hasOne('\app\api\model\hospital\Hospital', 'id', 'hospital_id');
    }

    public function orderTube()
    {
        return $this->hasMany('\app\api\model\order\OrderTube', 'order_master_id', 'id')->with(['biaobenRongqi'])->where('isdelete',10);
    }

    public function doctor(){
        return $this->hasOne('\app\api\model\User', 'id', 'user_id')->field('id,nickname,mobile,address');
    }

    public function orderReport(){
        return $this->hasMany('OrderReport', 'order_id', 'id');
    }


}
